#!/bin/bash

# postinst script for driver-hyperv-pv
#
# see: dh_installdeb(1)

#set -e

# summary of how this script can be called:
#        * <postinst> \`configure' <most-recently-configured-version>
#        * <old-postinst> \`abort-upgrade' <new version>
#        * <conflictor's-postinst> \`abort-remove' \`in-favour' <package>
#          <new-version>
#        * <postinst> \`abort-remove'
#        * <deconfigured's-postinst> \`abort-deconfigure' \`in-favour'
#          <failed-install-package> <version> \`removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

PATH=/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

if [ "${BASEDIR:=/}" = "/" ]; then
    BASEDIR=""
else
    BASEDIR_OPT="-b $BASEDIR"
fi

case "$1" in
    configure)
        if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/hv_netvsc.conf ] && rm -f $BASEDIR/kernel/drv/hv_netvsc.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/hv_netvsc.conf"
	    chown root:sys "$BASEDIR/kernel/drv/hv_netvsc.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/hv_vmbus.conf ] && rm -f $BASEDIR/kernel/drv/hv_vmbus.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/hv_vmbus.conf"
	    chown root:sys "$BASEDIR/kernel/drv/hv_vmbus.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/hv_heartbeat ] && rm -f $BASEDIR/kernel/drv/amd64/hv_heartbeat
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/hv_heartbeat"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/hv_heartbeat"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/hv_kvp ] && rm -f $BASEDIR/kernel/drv/amd64/hv_kvp
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/hv_kvp"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/hv_kvp"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/hv_netvsc ] && rm -f $BASEDIR/kernel/drv/amd64/hv_netvsc
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/hv_netvsc"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/hv_netvsc"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/hv_shutdown ] && rm -f $BASEDIR/kernel/drv/amd64/hv_shutdown
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/hv_shutdown"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/hv_shutdown"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/hv_storvsc ] && rm -f $BASEDIR/kernel/drv/amd64/hv_storvsc
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/hv_storvsc"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/hv_storvsc"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/hv_timesync ] && rm -f $BASEDIR/kernel/drv/amd64/hv_timesync
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/hv_timesync"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/hv_timesync"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/hv_vmbus ] && rm -f $BASEDIR/kernel/drv/amd64/hv_vmbus
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/hv_vmbus"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/hv_vmbus"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/misc/amd64/hyperv ] && rm -f $BASEDIR/kernel/misc/amd64/hyperv
	else
	    chmod 0755 "$BASEDIR/kernel/misc/amd64/hyperv"
	    chown root:sys "$BASEDIR/kernel/misc/amd64/hyperv"
	fi
	[ "$ZONEINST" = "1" ] || (grep -c "^hv_storvsc " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -c 'scsi-self-identifying' hv_storvsc ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^hv_heartbeat " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  hv_heartbeat ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^hv_kvp " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m '* 0600 root sys' hv_kvp ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^hv_netvsc " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  hv_netvsc ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^hv_shutdown " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  hv_shutdown ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^hv_timesync " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  hv_timesync ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^hv_vmbus " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  hv_vmbus ) )
	
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac



# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

